# 定制app表
{mongoose} = require "./mongodb"
CustomAppSchema = new mongoose.Schema({
    name: String
    version: String
    product_id: String # 产品ID
    product_name: String # 产品名称
    user_id: String #用户id
    certificate_id: String #证书id
    status: String #打包状态 0 已完成 1 打包失败  2 打包中
    platform: String # 类型 ios/android
    down_url: String # 包下载地址
    task_id: String # 打包任务id
    stateurl: String # 任务状态检查url
    modify_person: String
    create_date: type: Date, default: mongoose.dateHandle # 创建时间
    modify_date: type: Date, default: mongoose.dateHandle # 修改时间

});

CustomAppModel = mongoose.model('custom_app', CustomAppSchema)

CustomApp = ->

 # [save description] 插入记录
CustomApp.prototype.save = (obj, callback) ->
    instance = new CustomAppModel(obj)
    instance.save (err, obj, numberAffected) ->
        callback(err, obj, numberAffected)

# [findOne description] 按条件查找对象 返回第一个
CustomApp.prototype.findOne = (conditions, callback) ->
  # console.log "arguments size: #{arguments.length}"
  CustomAppModel.findOne.apply CustomAppModel, arguments

# [findOne description] 按条件查找对象 返回集合
# @param conditions 条件
# @param fields seletced字段
CustomApp.prototype.find = (conditions, fields, options, callback) ->
  CustomAppModel.find.apply CustomAppModel, arguments

#查询所有
CustomApp.prototype.findAll = (callback) ->
  this.find {}, callback

# [findOne description] 数量
CustomApp.prototype.count = (conditions, callback) ->
  CustomAppModel.count.apply CustomAppModel, arguments

 # [list description] 分页查询所有记录
 # @param  {[Number]}   page      [description] 第几页
 # @param  {[Number]}   pageLimit [description] 每页限制数
 # @param  {[string]}   sortParam [description] 排序字段
CustomApp.prototype.list = (page, pageLimit, sequence, callback) ->
  if sequence
    query = CustomAppModel.find ({})
    .select "name version logo splash user_id  create_date modify_date "
    .sort "-#{sequence}"
    .limit (pageLimit)
    .skip (page - 1 ) * pageLimit
    .exec (err, obj) ->
      callback(err, obj)
  else
    query = CustomAppModel.find ({})
    .select "name version logo splash user_id  create_date modify_date "
    .limit (pageLimit)
    .skip (page - 1 ) * pageLimit
    .exec (err, obj) ->
      callback(err, obj)

CustomApp.prototype.findOneAndUpdate = (conditions, update, callback) ->
  # console.log "arguments size: #{arguments.length}"
  CustomAppModel.findOneAndUpdate.apply CustomAppModel, arguments

# [findOne description] 更新文档
# @param  {[obj]}   conditions      [description] 根据某个条件 {_id: id}，查找记录
# @param  {string} field [description] option
# @param  {[obj]} options [description] option
CustomApp.prototype.update = (conditions, field, options, callback) ->
  CustomAppModel.update.apply CustomAppModel, arguments

CustomApp.prototype.findOneAndRemove = (conditions, options, callback) ->
  # console.log "arguments size: #{arguments.length}"
  CustomAppModel.findOneAndRemove.apply CustomAppModel, arguments

# [remove description] 移除记录
# @param  {[type]}   conditions      [description] 根据某个条件 {_id: id}，删除记录
CustomApp.prototype.remove = (conditions, callback) ->
  CustomAppModel.remove.apply pageTemplates, arguments

module.exports = new CustomApp()
